import { getCurrentTime } from '../utils/baseUtils';

class LabelBase {
    constructor(label_format) {
        // 定义标注文件的格式
        this.label_format = label_format;

        // 底层的数据存储
        this.labels = [];
        this.saved = true;

        // canvas相关绘制
        this.canvas = null;
        this.image = null;
        this.tools_callback = {};

        // label ui相关绘制
        this.label_panel = null;
    }

    ///====================以下定义geom显示相关的处理函数========================
    // 在canvas中渲染结果
    bindCanvas(canvas, image, label_tools) {
        this.canvas = canvas;
        this.image = image;
        if (label_tools) {
            for (let tool_name in this.tools_callback) {
                let tool = label_tools[tool_name];
                let callback = this.tools_callback[tool_name];
                if (tool) {
                    tool.setCallback(callback);
                }
            }
        }
    }

    // 设置对象是否可以编辑
    setEditableMode(editable = true) { }

    registerObjectEvents(object) {
        // console.log(`registerObjectEvents on object ${object.label_id}-${object.seg_index}, type = `, object.type);
        object.on('selected', this.onLabelSelected.bind(this));
        object.on('modified', this.onLabelModified.bind(this));
        object.on('mouseover', this.onLabelMouseover.bind(this));
        object.on('mouseout', this.onLabelMouseout.bind(this));
    }

    onLabelSelected(event) {
        if (event.target) {
            this.selectLabel(event.target.label_id, event.target);
        }
    }

    onLabelModified(event) { }

    onLabelMouseover(event) {
        // console.log(`onLabelMouseover event = `, event);
    }

    onLabelMouseout(event) {
        // console.log(`onLabelMouseout event = `, event);
    }

    selectLabel(target) { }

    unselectLabel() { }

    deleteSelection() { }

    ///========================以下定义label-panel相关的处理函数========================
    // 绑定vue中的labelpanel对象
    bindLabelPanel(label_panel) {
        this.label_panel = label_panel;
        this.label_panel && this.label_panel.loadLabels(this.labels);
    }

    ///========================以下定义数据相关的处理函数========================
    // 添加一个文字检测的标注结果
    addLabel(data) { }

    // 更新对应的labelid信息，可以更新poly或者text文本
    updateLabel(data) { }

    // 删除对应的labelid数据
    deleteLabel(labelid) { }

    // 清空所有的标注对象
    clearLabels() { }

    // 从json结果构建
    fromJson(json_data) { }

    // 导出json格式的结果
    toJson() { }

    ///========================以下定义数据类型========================
    labelOfBase(id, type, data) {
        let instance = {
            id: id,
            type: type,
            method: 'manuallabel',
            author: '',
            timestamp: getCurrentTime(),
            score: 1.0,
            data: data
        };
        return instance;
    }
}

// 工厂函数 createLabel 根据类型动态创建标注结果对象
async function createLabel(algo_type, label_format) {
    // console.log('createLabel algo_type:', algo_type, 'label_format:', label_format);
    try {
        let LabelClass;
        ({ default: LabelClass } = await import(`../../components/ToolPages/${algo_type.supertype}/${algo_type.type}/Label.js`));
        return new LabelClass(label_format);
    } catch (error) {
        console.error('Failed to load label result module', error);
        throw new Error('Could not create label result object');
    }
}

export { createLabel, LabelBase };
